﻿@using Telerik.Web.Mvc.UI
@model MvcFront.Models.FieldTemplateEditModel
@{
    ViewBag.Title = "FieldTemplateEditModel";
}
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.FieldTemplateId)
@Html.HiddenFor(model => model.DocTemplateId)
<div class="editor-label">
    @Html.LabelFor(model => model.FieldTemplateName)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.FieldTemplateName)
    @Html.ValidationMessageFor(model => model.FieldTemplateName)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.FieldType)
</div>
<div class="editor-field">
    @Html.Telerik().DropDownList().Name("FieldType").Items(item =>
    {
        item.Add().Text("Да/Нет").Value("0").Selected(@Model.FieldType == 0);
        item.Add().Text("Число").Value("1").Selected(@Model.FieldType == 1);
        item.Add().Text("Строка").Value("2").Selected(@Model.FieldType == 2);
        item.Add().Text("Вычислимое").Value("3").Selected(@Model.FieldType == 3);
        if (Model.CanBePlaned) { item.Add().Text("Планируемое").Value("4").Selected(@Model.FieldType == 4); }
    }).ClientEvents(events => events.OnChange("FieldType_onChange"))
    @Html.ValidationMessageFor(model => model.FieldType)
</div>
<div id="IntBlockId">
    <div class="editor-label">
        @Html.LabelFor(model => model.IsInteger)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.IsInteger)
        @Html.ValidationMessageFor(model => model.IsInteger)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.IsRestricted)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.IsRestricted)
        @Html.ValidationMessageFor(model => model.IsRestricted)
    </div>
    <div id="RestictId">
        <div class="editor-label">
            @Html.LabelFor(model => model.MaxVal)
        </div>
        <div class="editor-field">
            @Html.Telerik().NumericTextBoxFor(model => model.MaxVal).EmptyMessage("Введите число")
            @Html.ValidationMessageFor(model => model.MaxVal)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.MinVal)
        </div>
        <div class="editor-field">
            @Html.Telerik().NumericTextBoxFor(model => model.MinVal).EmptyMessage("Введите число")
            @Html.ValidationMessageFor(model => model.MinVal)
        </div>
    </div>
</div>
<div id="CalcBlockId">
    <div class="editor-label">
        @Html.LabelFor(model => model.OperationExpression)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OperationExpression)
        <div class="display-label">
            Поля Формы</div>
        @(Html.Telerik().Grid<MvcFront.Models.FieldTemplateListViewModel>()
           .DataBinding(dataBinding => dataBinding.Ajax().Select("_FieldTemplateListNotUsedForCalc", "DocTemplateList", new { docTemplID = Model.DocTemplateId, fieldTemplId = Model.FieldTemplateId }))
           .Name("NotAddedFieldTemplatesListGrid")

           .ClientEvents(events => events.OnComplete("onComplete"))
           .HtmlAttributes(new { style = Model.FieldTemplateId > 0 ? "display: block; " : "display: none; " })
           .Columns(columns =>
                        {
                            columns.AutoGenerate(column =>
                                                     {
                                                         if (column.Member == "FieldTemplateId" || column.Member == "OperationTypeText" || column.Member == "FieldTypeText")
                                                             column.Visible = false;
                                                     });
                            columns.Command(command => command.Custom("AddFieldToCalc")
                                                           .Text("Добавить для вычисления")
                                                           .DataRouteValues(route => route.Add(o => o.FieldTemplateId).RouteKey("fieldTemplateId"))
                                                           .Ajax(true)
                                                           .Action("AddFieldToCalc", "DocTemplateList")
                                                           .HtmlAttributes(new { style = "text-align: center" })).Width(160);
                        })
              )
        <br />
        @(Html.Telerik().Grid<MvcFront.Models.FieldTemplateListViewModel>()
           .DataBinding(dataBinding => dataBinding.Ajax().Select("_FieldTemplateListUsedForCalc", "DocTemplateList", new { docTemplID = Model.DocTemplateId, fieldTemplId = Model.FieldTemplateId }))
           .Name("AddedFieldTemplatesListGrid")
           .ClientEvents(events => events.OnComplete("onComplete"))
           .HtmlAttributes(new { style = Model.FieldTemplateId > 0 ? "display: block; " : "display: none; " })
           .Columns(columns =>
                        {
                            columns.AutoGenerate(column =>
                                                     {
                                                         if (column.Member == "FieldTypeText")
                                                             column.Visible = false;
                                                     });
                            columns.Command(command => command.Custom("DeleteFieldFromCalc")
                                                           .Text("Удалить из вычисления")
                                                           .DataRouteValues(route => route.Add(o => o.FieldTemplateId).RouteKey("fieldTemplateId"))
                                                           .Ajax(true)
                                                           .Action("DeleteFieldFromCalc", "DocTemplateList")
                                                           .HtmlAttributes(new { style = "text-align: center" })).Width(160);
                        })
              )
    </div>
</div>
<div id="PlannedBlockId">
    <div class="editor-label">
        @Html.LabelFor(model => model.FactFieldTemplateId)
    </div>
    <div class="editor-field">
        @(Html.Telerik().DropDownListFor(x => x.FactFieldTemplateId).ClientEvents(events => events.OnLoad("onFactLoad"))
           .DataBinding(binding => binding.Ajax().Select("_FactFieldTemplatesList", "DocTemplateList", new { docTemplID = Model.DocTemplateId, factFieldId = Model.FactFieldTemplateId }))
              )
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $('#IntBlockId').hide();
        $('#RestictId').hide();
        $('#PlannedBlockId').hide();
        FieldType_onChange();

        if ($('#IsRestricted').is(':checked')) {
            $('#RestictId').show();
        } else {
            $('#RestictId').hide();
        }
        $('#IsRestricted').click(function() {
            if ($('#IsRestricted').is(':checked')) {
                $('#RestictId').show();
            } else {
                $('#RestictId').hide();
            }
        });
    });
    function FieldType_onChange() {
        var ddl = $('#FieldType');
        if (ddl.val() == 1) {
            $('#IntBlockId').show();
            $('#CalcBlockId').hide();
            $('#PlannedBlockId').hide();
        } else {
            $('#IntBlockId').hide();
        }

        if (ddl.val() == 3) {
            if ('@(Model.FieldTemplateId > 0)' != 'False') {
                $('#CalcBlockId').show();
            }
            $('#IntBlockId').hide();
            $('#PlannedBlockId').hide();
        } else {
            $('#CalcBlockId').hide();
        }

        if (ddl.val() == 4) {
            $('#PlannedBlockId').show();
            $('#CalcBlockId').hide();
            $('#IntBlockId').hide();
        } else {
            $('#PlannedBlockId').hide();
        }
    }

    function onComplete(e) {
        if (e.name == "AddFieldToCalc" || e.name == "DeleteFieldFromCalc") {
            var grid1 = $('#AddedFieldTemplatesListGrid').data("tGrid");
            if (grid1 != null) {
                grid1.rebind();
            }
            var grid2 = $('#NotAddedFieldTemplatesListGrid').data("tGrid");
            if (grid2 != null) {

                grid2.rebind();
            }
        }
    }

    function onFactLoad(e) {
        $(this).data('tDropDownList').fill();
    }
</script>
